package com.zjr.service;

import java.sql.*;
import java.util.ArrayList;

import com.zjr.db.DBConnection;
import com.zjr.domain.Log;

public class LogService {
	private Connection conn = null;
	private PreparedStatement stmt = null;
	private ResultSet rs = null;
	private String sql = null;
	
	//添加日志
	public boolean addLog(Log log){
		boolean flag = false;
		try {
			conn = DBConnection.getConnection();
			sql = "insert into log (place, duration, submittime, username, state, content)"
					+ "values (?, ?, ?, ?, ?, ?)";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, log.getPlace());
			stmt.setFloat(2, log.getDuration());
			stmt.setString(3, log.getSubmittime());
			stmt.setString(4, log.getUsername());
			stmt.setInt(5, log.getState());
			stmt.setString(6, log.getContext());
			if(stmt.executeUpdate() == 1){
				flag = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.Close(rs, stmt, conn);
		}
		
		return flag;
	}
	
	public ArrayList<Log> queryAllLogs(){
		ArrayList<Log> logs = new ArrayList<Log>();
		try {
			conn = DBConnection.getConnection();
			sql = "select * from log";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while(rs.next()){
				Log log = new Log();
				log.setLid(rs.getInt("lid"));
				log.setUsername(rs.getString("username"));
				log.setPlace(rs.getString("place"));
				log.setDuration(rs.getFloat("duration"));
				log.setSubmittime(rs.getString("submittime"));
				log.setState(rs.getInt("state"));
				log.setContext(rs.getString("content"));
				logs.add(log);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.Close(rs, stmt, conn);
		}
		
		return logs;
	}
	
	public ArrayList<Log> queryLogsByUsername(String username){
		ArrayList<Log> logs = new ArrayList<Log>();
		try {
			conn = DBConnection.getConnection();
			sql = "select * from log where username=?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, username);
			rs = stmt.executeQuery();
			while(rs.next()){
				Log log = new Log();
				log.setLid(rs.getInt("lid"));
				log.setUsername(rs.getString("username"));
				log.setPlace(rs.getString("place"));
				log.setDuration(rs.getFloat("duration"));
				log.setSubmittime(rs.getString("submittime"));
				log.setState(rs.getInt("state"));
				log.setContext(rs.getString("content"));
				logs.add(log);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.Close(rs, stmt, conn);
		}
		
		return logs;
	}
	
	public Log findLogById(int lid){
		Log log = null;
		try {
			conn = DBConnection.getConnection();
			sql = "select * from log where lid=?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, lid);
			rs = stmt.executeQuery();
			if(rs.next()){
				log = new Log();
				log.setLid(rs.getInt("lid"));
				log.setPlace(rs.getString("place"));
				log.setUsername(rs.getString("username"));
				log.setDuration(rs.getFloat("duration"));
				log.setSubmittime(rs.getString("submittime"));
				log.setState(rs.getInt("state"));
				log.setContext(rs.getString("content"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.Close(rs, stmt, conn);
		}
		
		return log;
	}
	
	public boolean modifyLog(Log log){
		boolean flag = false;
		try {
			conn = DBConnection.getConnection();
			sql = "update log set content=?, state=? where lid=?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, log.getContext());
			stmt.setInt(2, log.getState());
			stmt.setInt(3, log.getLid());
			if(stmt.executeUpdate() > 0){
				flag = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.Close(rs, stmt, conn);
		}
		
		return flag;
	}
	
	public void deleteLogByLid(int lid){
		try {
			conn = DBConnection.getConnection();
			sql = "delete from log where lid = ?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, lid);
			stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.Close(rs, stmt, conn);
		}
	}
}
